import Vue from 'vue'
import Vuex from 'vuex'

const util = require("@/utils/util.js");

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		//app版本
		version: "1.5.1",
		//当前是否有网络连接
		networkConnected: true,
		position: {
			address: '',
			name: '',
			province: '',
			city: '',
			cityName: '',
			countyName: '',
			latitude: '',
			longitude: ''
		}
	},
	getters: {
		position: state => state.position
	},
	mutations: {
		networkChange(state, payload) {
			state.networkConnected = payload.isConnected
		},
		SET_POSITION(state, data) {
			state.position = Object.assign({}, data)
		}
	},
	actions: {
		async autoLocation({
							   commit
						   }) {
			console.log('autoLocation');
			let that = this;
			if (this.getters.position && this.getters.position.latitude) {
				// commit('SET_POSITION', this.getters.position)
				return;
			}
			// 定位
			let resLocate = await util.wxLocation();
			console.log('resLocate', resLocate);
			if (resLocate.latitude) {
				commit('SET_POSITION', resLocate)
			}
		}
	}
})

export default store
